#include "CarStop.h"


std::string GetTimeStr(time_t time)
{
    tm *m = localtime(&time);
    char s[1024] = {0};
    sprintf(s,"%d年%d月%d日 %d:%d:%d\n",m->tm_year + 1900,m->tm_mon + 1,m->tm_mday,m->tm_hour,m->tm_min,m->tm_sec) ;
    return std::string(s);
}

CarStop::~CarStop()
{
    while(stop.IsEmpty() == false)
    {
        Car *c = (Car*)*stop.GetTop();
        delete c;
        stop.Pop();
    }
    while(pavement.IsEmpty() == false)
    {
        Car *c = (Car*)*pavement.GetFront();
        delete c;
        pavement.Pop();
    }

}

void CarStop::ComeCar()
{
    Car *c = new Car("");
    std::cout<<"请输入车牌：";
    std::cin>>c->Name;
    c->arriveTime = time(nullptr);//获取当前时间
    std::cout<<"车辆|"<<c->Name<<"|在"<<GetTimeStr(c->arriveTime)<<"到达停车场\n";

    if(stop.Size() == size)
    {
        std::cout<<"停车场已满，"<<c->Name<<"停在便道上第"<<pavement.Size() + 1<<std::endl;
        pavement.Push(c);
    }
    else
    {
        std::cout<<"停车场未停满,停入停车场第"<<stop.Size()+1<<"个位置"<<std::endl;
        stop.Push(c);
    }
   
}

void CarStop::LeaveCar()
{
    std::cout<<"请输入要离开的车辆：";
    std::string LeaveCarName;
    std::cin>>LeaveCarName;

    LinkStack tempStop;//临时栈
    while(stop.IsEmpty() == false)
    {
        Car *c = (Car*)*stop.GetTop();
        stop.Pop();
        if(c->Name == LeaveCarName)
        {
            c->leaveTime = time(nullptr);
            double money = 1.0*  (c->leaveTime - c->arriveTime) / 3600 * 200;
            std::cout<<"车辆|"<<c->Name<<"|于"
                <<GetTimeStr(c->leaveTime)
                <<"离开停车场，停车费用|"
                <<money<<"|元"
                <<std::endl;
            delete c;
            break;
        }
        tempStop.Push(c);
    }
    while(tempStop.IsEmpty() == false)
    {
        stop.Push(*tempStop.GetTop());
        tempStop.Pop();
    }
    if(stop.Size() < size && pavement.IsEmpty() == false)
    {
        Car *c = (Car*)*pavement.GetFront();
        c->arriveTime = time(nullptr);
        std::cout<<"便道上的车|"<<c->Name<<"|在"<<GetTimeStr(c->arriveTime)<<"进入停车场"<<std::endl;
        pavement.Pop();
        stop.Push(c);
    }

}

void CarStop::show()
{
    LinkStack tempStop;//临时栈
    while(stop.IsEmpty() == false)
    {
        tempStop.Push(*stop.GetTop());
        stop.Pop();
    }
    while(tempStop.IsEmpty() == false)
    {
        Car *c = (Car*)*tempStop.GetTop();
        std::cout<<"🚗|"<<c->Name<<"|"<<std::endl;
        stop.Push(c);
        tempStop.Pop();
    }
    std::cout<<std::endl;
    LinkQueue tempPavement;
    while(pavement.IsEmpty() == false)
    {
        tempPavement.Push(*pavement.GetFront());
        pavement.Pop();
    }
    while(tempPavement.IsEmpty() == false)
    {
        Car *c = (Car*)*tempPavement.GetFront();
        std::cout<<"      🚗|"<<c->Name<<"|"<<" ";
        pavement.Push(c);
        tempPavement.Pop();
    }
    std::cout<<std::endl;
}
